package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.wf.captcha.SpecCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private StringRedisTemplate  stringRedisTemplate;
    @Override
    public CaptchaVo getCaptcha() {
        //1.生成key（UUID）
        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID().toString();
        //2.生成验证码
        //定义宽、高和位数
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 1);
        //设置验证码类型（字母数字混合）
        specCaptcha.setCharType(SpecCaptcha.TYPE_DEFAULT);
        String s = specCaptcha.text().toLowerCase();
        //生成图片
        String image = specCaptcha.toBase64();
        stringRedisTemplate.opsForValue().set(key,s,RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);
        return new CaptchaVo(image,key);

    }
}
